/*
  @author NormanMises
  @date 2023/6/6 - 20:49
*/

package com.example.demo.service.impl.user.account;

import com.example.demo.pojo.user;
import com.example.demo.service.impl.utils.UserDetaileImpl;
import com.example.demo.service.user.account.LoginService;
import com.example.demo.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Override
    public Map<String, String> getToken(String username, String password) {

        UsernamePasswordAuthenticationToken authenticationToken=new UsernamePasswordAuthenticationToken(username,password);
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        UserDetaileImpl loginUser=(UserDetaileImpl) authenticate.getPrincipal();
        user user=loginUser.getUser();
        String jwt= JwtUtil.createJWT(user.getId().toString());
        Map<String,String> map=new HashMap<>();
        map.put("message","success");
        map.put("token",jwt);
        return map;
    }
}
